<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>任务清单</title>
    <style>
        * {
            /* margin: 0;
            padding: 0; */
            list-style: none;
        }

        .app {
            position: fixed;
            left: 50%;
            top: 50%;
            margin-top: -7%;
            margin-left: -7%;
        }
        input {
            float: left;
            height: 15px;
            outline:none
        }

        button {
            box-sizing: content-box;
            height: 15px;
            text-align: center;
        }

        span {
            font-weight: normal;
            margin-left: 50px;
        }
        .box{
            margin-right: 20px;
            width: 15px;
            height: 15px;
            vertical-align: middle;
        }
        .btn{
            margin-left: 20px;
        }
    </style>
</head>

<body>
    <div id="app" class="app">
        <input v-model="val" type="text">
        <button @click="valItem" :disabled="flag">添加</button>

        <h1>没有完成 <span>{{len}}</span></h1>
        <ul>
            <li v-for="(item,index) in arr" v-show="!item.checked">
                <input v-model="item.checked" type="checkbox" class="box">
                {{ item.val }}
                <button @click="del(index)" class="btn">删除</button>
            </li>
        </ul>

        <h1>已经完成 <span>{{arr.length - len}}</span></h1>
        <ul>
            <li v-for="(item,index) in arr" v-show="item.checked">
                <input v-model="item.checked" type="checkbox" class="box">
                {{ item.val }}
                <button @click="del(index)" class="btn">删除</button>
            </li>
        </ul>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script>
        new Vue({
            el: "#app",
            // 存储数据
            data: {
                // input输入的值
                val: "",
                // 用来存储输入的值,就是任务
                arr: [],
                flag:true,
            },
            // 函数的包装
            methods: {
                valItem() {
                    this.arr.push({
                        val: this.val,
                        checked: false
                    })
                    this.val = ""
 
                },
                del(a){
                    this.arr.splice(a,1)
                }
            },
            // 计算属性
            computed: {
                len() {
                    return this.arr.filter((a) => {
                        return a.checked === false
                    }).length
                },
            },
            watch:{
                val(newValue,oldValue){
                    if(newValue.length > 0){
                        this.flag = false
                    }else{
                        this.flag = true
                    }
                }
            }
        })
    </script>
</body>

</html>